<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <style type="text/css">
    </style>
    <link rel="stylesheet" type="text/css" href="style/style.css"/>
	<script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript">
	function browser()
	{
		if($.browser.msie) return 0;
		if($.browser.mozilla) return 1;
		if($.browser.opera) return 2;
		if($.browser.webkit) return 3;
	}
	
	function game() {
		var ctx = $('#cv').get(0).getContext('2d');
		var mctx = $('#cv').get(0).getContext('2d');
		var canvas = document.getElementById('cv');
		
		var d = document.getElementById('restart');
		if(d) document.getElementById('box').removeChild(d);
		
		var sperms = [
			{ x:  0, y:  0, r: 30, ds:1, img:1 },
			{ x: 800, y:  0, r: 30, ds:1, img:1 },
			{ x: 800, y: 600, r: 30, ds:1, img:1 },
			{ x:  0, y: 600, r: 30, ds:1, img: 1 },
		];
		
		var img = new Image();
		img.src="images/object/egg.png";
		
		var aimg = new Image();
		aimg.src="images/object/egg_active.png";
		//Sound
		var FoneSound;
		var SpermSound;
		var TotalDeadSound;
		
		var ext = ".mp3";
		var b = browser();
		if( b == 2 || b == 1) ext = ".ogg";
		
		//fone misic init total_dead
		FoneSound = new Audio('sound/fone' + ext);
		FoneSound.volume = 0.1;
		
		SpermSound = new Audio('sound/sperms' + ext);
		SpermSound.volume = 0.5;
		
		TDSound = new Audio('sound/total_dead' + ext);
		TDSound.volume = 0.5;
		
		
		var sinops =		{x: 650, y: -50};
		var telomaLarge =	{x: 100, y: 80, k:1 };
		var telomaMedium =	{x: 680, y: 450, k:1 };
		var telomaSmall =	{x: 80, y: 360, k: 1 };
		var pionisLarge = 	{x: 350, y: 30};
		var pionisSmall = 	{x: 260, y: 450};
		var letisaLarge = 	{x: -60, y: 450};
		var letisaMedium1 = {x: 640, y: 270};
		var letisaMedium2 = {x: 400, y: 470};
		var letisaSmall = 	{x: 200, y: 210};
		var Sillepsis = {x: 85, y: 199}
		var Start = {x: 400, y: 300}
				
		var sinopsimg = new Image();
			sinopsimg.src="images/object/sinops.png";
		
		var telomaLargeImg = new Image();
			telomaLargeImg .src="images/object/teloma_large.png";
		var telomaMediumImg = new Image();
			telomaMediumImg .src="images/object/teloma_medium.png";
		var telomaSmallImg = new Image();
			telomaSmallImg .src="images/object/teloma_small.png";
			
		var telomaLargeImgAc = new Image();
			telomaLargeImgAc.src="images/object/teloma_large_ac.png";
		var telomaMediumImgAc = new Image();
			telomaMediumImgAc.src="images/object/teloma_medium_ac.png";
		var telomaSmallImgAc = new Image();
			telomaSmallImgAc.src="images/object/teloma_small_ac.png";
		
		var pionisLargeImg = new Image();
			pionisLargeImg .src="images/object/pinios_large.png";
		var pionisSmallImg = new Image();
			pionisSmallImg .src="images/object/pinios_small.png";
		
		var letisaLargeImg = new Image();
			letisaLargeImg .src="images/object/letisa_large.png";
		var letisaMediumImg = new Image();
			letisaMediumImg .src="images/object/letisa_medium.png";
		var letisaSmallImg = new Image();
			letisaSmallImg .src="images/object/letisa_small.png";
			
		var SillepsisImg = new Image();
			SillepsisImg .src="images/text/sillepsis.PNG";
		var StartImg = new Image();
			StartImg .src="images/text/sillepsis.PNG";
	
		
		//=================
		var simg = new Array();
		for(var i = 1; i < 9; i++)
		{
			var ximg = new Image();
			ximg.src = "images/p/" + i +".png";
			simg.push(ximg);
		}
		
		//var die = new Array();
		var dimg = new Array();
		for(var i = 1; i < 14; i++)
		{
			var ximg = new Image();
			ximg.src = "images/d/" + i +".png";
			dimg.push(ximg);
		}
		
		var digits = new Array();
		
		for(var i=0; i <=9; i++)
		{
			var ximg = new Image();
			ximg.src = "images/text/num/" + i + ".png";
			digits.push(ximg);
		}

		var mainCircle = { x: 400, y: 300, r: 76.5 };
		var runtime = window.setInterval(recalc, 50);
		
		var mmax = 7;
		var ds = 1;
		
		var xO = mainCircle.x;
		var yO = mainCircle.y;
		
		var mx = 0;
		var my = 0;
		
		var sr = 80;
		
		$('#cv').mousemove(function(e){ 
			mx = e.clientX;
			my = e.clientY;

			if (e.pageX || e.pageY) {
			  x = e.pageX;
			  y = e.pageY;
			}
			else {
			  mx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			  my = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
			
			mx -= canvas.offsetLeft;
			my -= canvas.offsetTop;
		});
		
		$('#cv').click(function(e){ 
			
			var x = e.clientX;
			var y = e.clientY;

			if (e.pageX || e.pageY) {
			  x = e.pageX;
			  y = e.pageY;
			}
			else {
			  x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			  y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
			
			x -= canvas.offsetLeft;
			y -= canvas.offsetTop;
			
			var toDel = new Array();
			var d = false;
			for(i =0; i < sperms.length; i++){
				
				var s = sperms[i];
				
				if(!s.die){
					var xC = s.x;
					var yC = s.y;
					var R = s.r;
				
					var inC = (x-xC)*(x-xC) + (y-yC)*(y-yC) < R*R;

					s.die = inC;
					if (inC){
						SpermSound.currentTime = 0;
       					SpermSound.play();
						d = true;
						}
					s.tag = 1;
				}
			}
			
			if(!d)
			{
				if(telomaLarge.k > 0 && inrect(mx, my, telomaLarge.x, telomaLarge.y, 104, 97))
				{
					 death_all(telomaLarge);
					 TDSound.currentTime = 0;
       				 TDSound.play();
				}
				if(telomaMedium.k > 0 && inrect(mx, my, telomaMedium.x, telomaMedium.y, 61, 64))
				{
					death_all(telomaMedium);
					TDSound.currentTime = 0;
       				TDSound.play();
				}
				if(telomaSmall.k > 0 && inrect(mx, my, telomaSmall.x, telomaSmall.y, 53, 49))
				{
					death_all(telomaSmall);
					TDSound.currentTime = 0;
       				TDSound.play();
				}
			}
		});
		
		function death()
		{
			var o = new Array();
			for(var i=0; i< sperms.length; i++){
				var d = sperms[i];
				if(d.die && d.tag >= 13) { score++; mmax += 1;}
				else o.push(d);
			}
			
			sperms = o;
			log(sperms.length);
		}
		
		function death_all(teloma)
		{
			teloma.k--;
			for(var i=0; i< sperms.length; i++){
				var d = sperms[i];
				d.die = true;
				d.tag = 1;
			}
		}
		
		function PlayFoneSound()
		{
			FoneSound.addEventListener('ended', function()
			 {
					this.currentTime = 0;
				if (timeActive < 100) {	
					this.play();
				}
			 }, false);
				FoneSound.play();
			
		}
		PlayFoneSound();
		
	
		function log(v)
		{
			$('#log').text(v);
		}
		
		function born()
		{
			if(sperms.length < mmax){
				var xxr = Math.abs(Math.random());
				var h = ctx.canvas.width + ctx.canvas.height;
				var r = xxr * h;
				var a = r * 2;
				var x = 0; var y = 0;
				
				var br = r;
				
				if( a < 800 )
				{
					x = a;
					y = 0;
				}
				
				if( a > 800 && a < 1400)
				{
					r = a - 800;
					x = 800;
					y = r;
				}
				
				if( a > 1400 && a < 2200)
				{
					x = a - 1400;
					y = 600;
				}
				
				if( a > 2200)
				{
					r = a - 2200;
					x = 0;
					y = 600 - r;
				}
				
				sperms.push({ x: x, y: y, r:30, ds:ds, img:1});
				
				if( mmax > 15 + 20*(ds-1))
				{
					mmax = 4;
					ds += 0.05;
				}
			}
		}

		var orbitSpeed = 0.002;
		var angle = 0;
		var score = 0;
			
		function recalc()
		{
			var x = mainCircle.x;
			var y = mainCircle.y;
			var a = false;

			for(i =0; i < sperms.length; i++){
				var x1 = sperms[i].x;
				var y1 = sperms[i].y;
				var ds = sperms[i].ds;
				
				var c = Math.sqrt( (x - x1)*(x - x1) + (y - y1)*(y - y1) );
				
				var k = Math.abs(ds/c);
				
				if(!sperms[i].die)
				{
					if(c > mainCircle.r)
					{

						var dx = (x - x1)*k;
						var dy = (y - y1)*k;
					
						sperms[i].x += dx;
						sperms[i].y += dy;
					}
					else
					{
						a = true;
					}
				}
			}
			
			mainCircle.active = a;
			
			if(a == true)
			{	
				timeActive++;
			}
			else
			{
				timeActive = 0;
			}
			
			mainCircle.x = xO + sr * Math.sin(0.6*angle);
			mainCircle.y = yO + sr * Math.sin(0.4*angle);
			
			angle += orbitSpeed;
			
			born();
			draw();
			death();
		}

		function rdraw(x, y, angle, d)
		{
			/*
			ctx.lineWidth = 5;
			ctx.strokeStyle = 'red';
			ctx.rect(x, y, 100, 100);
			ctx.stroke();
			*/
			
			ctx.save();
			ctx.translate(x, y);
			ctx.beginPath();
			ctx.rotate(angle);
			d();
			ctx.stroke();
			ctx.restore();
		}
		
		function draw_digits(x, y, text)
		{
			var offset = x;
			for(var i=0; i< text.length; i++)
			{
				var digit = digits[parseInt(text[i])];
				ctx.drawImage(digit, offset, y, digit.width, digit.height);
				offset += (digit.width + 2);
			}
			ctx.stroke();
		}
		
		function inrect(mx, my, x, y, w, h)
		{
			var w2 = w/2; var h2 = h/2;
			return mx > x - w2 && mx < x + w2 && my > y - h2 && my < y + h2;
		}
		
		function draw()
		{
			if(timeActive >= 100)
			{
				//window.open(ctx.canvas.toDataURL());
				ctx.drawImage(SillepsisImg, Sillepsis.x, Sillepsis.y, 630, 101);
				ctx.stroke();
				clearInterval(runtime);
				
				var input = document.createElement('input');
				input.id = 'restart';
				input.type = 'submit';
				input.style.width = '179px';
				input.style.height = '61px';
				input.value = '';
				input.onclick = game;
				input.style.position = 'absolute';
				input.style.top = (500 + parseInt(canvas.offsetTop))+ 'px';
				input.style.left = (parseInt(canvas.offsetLeft) + 300) + 'px';
				document.getElementById('box').appendChild(input);

				return;
			}
			
			ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height );

			var tli = telomaLargeImg;
				if(telomaLarge.k > 0 && inrect(mx, my, telomaLarge.x, telomaLarge.y, 104, 97)){tli = telomaLargeImgAc;}
			
			var tmi = telomaMediumImg;
				if(telomaMedium.k > 0 && inrect(mx, my, telomaMedium.x, telomaMedium.y, 61, 64)){tmi = telomaMediumImgAc;}
			
			var tsi = telomaSmallImg;
				if(telomaSmall.k > 0 && inrect(mx, my, telomaSmall.x, telomaSmall.y, 53, 49)){tsi = telomaSmallImgAc;}
						
			rdraw(telomaLarge.x, telomaLarge.y, -angle, function(){	ctx.drawImage(tli, -52, -48, 104, 97); });
			rdraw(telomaMedium.x, telomaMedium.y, -5*angle, function(){	ctx.drawImage(tmi, -30, -32, 61, 64); });
			rdraw(telomaSmall.x, telomaSmall.y, 2*angle, function(){	ctx.drawImage(tsi, -27, -25, 53, 49); });
			
			ctx.drawImage(pionisLargeImg, pionisLarge.x, pionisLarge.y, 145, 142);
			ctx.drawImage(pionisSmallImg, pionisSmall.x, pionisSmall.y, 55, 54);
			
			rdraw(letisaMedium1.x, letisaMedium1.y, angle, function(){ctx.drawImage(letisaMediumImg, -64, -63, 128, 125); });
			
			ctx.drawImage(letisaMediumImg, letisaMedium2.x, letisaMedium2.y, 128, 125);
			ctx.drawImage(letisaSmallImg, letisaSmall.x, letisaSmall.y, 101, 104);
			
			rdraw(mainCircle.x, mainCircle.y, angle, function(){
				if(!mainCircle.active) ctx.drawImage(img, - mainCircle.r, - mainCircle.r, 153, 153);
				else ctx.drawImage(aimg, - mainCircle.r, - mainCircle.r, 153, 153);
			});
			
			for(i =0; i < sperms.length; i++)
			{
				var data = sperms[i];
				
				if(data.img >= 8) data.img = 1;
				
				var ang = Math.atan((mainCircle.y - data.y)/(mainCircle.x - data.x));
				ctx.save();
				ctx.translate(data.x, data.y);
				ctx.beginPath();
				ctx.rotate(ang + ( mainCircle.x - data.x > 0) * Math.PI);
				
				if(data.die) 
				{
					ctx.drawImage(dimg[data.tag], -data.r-7, -data.r-7, 40, 40);
					data.tag++;
				}
				else ctx.drawImage(simg[data.img], -data.r, - data.r, 133, 26);
				
				ctx.stroke();
				ctx.restore();

				data.img++;
			}
			
			ctx.drawImage(sinopsimg, sinops.x, sinops.y, 209, 178);
			ctx.drawImage(letisaLargeImg, letisaLarge.x, letisaLarge.y, 244, 251);
		
			draw_digits(15,5, score.toString());
			
			if(timeActive > 0)
			{
				var time = 99;
				ctx.font = '50px a_LCDNovaObl';
				ctx.textAlign = 'center';
				ctx.fillStyle = 'red';
				ctx.fillText(time-timeActive.toString(), ctx.canvas.width/2, 50);	
			}
		}
		
		
	   }
	
	function cgame()
	{
	try{ game();}catch(e){alert(e);}
	}
	
	 $(document).ready(cgame);

    </script>
 </head>
<body>
        <div id="box">
            <canvas width="800" height="600" id="cv" > </canvas>
            <div id=''></div>
        </div>
		<div id="log"></div>
</body>
</html>
